package com.shangxian.giveithome.controller;

import com.shangxian.giveithome.annoaction.CheckLoginStatus;
import com.shangxian.giveithome.code.CheckCode;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.community.Comment;
import com.shangxian.giveithome.entity.community.NoticeCenter;
import com.shangxian.giveithome.mapper.CommentMapper;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.ApiService;
import com.shangxian.giveithome.service.CommentService;
import com.shangxian.giveithome.utils.RedisUtils;
import com.shangxian.giveithome.utils.SenInfoCheckUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ApiService apiService;

    @Resource
    private CommentMapper commentMapper;

    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("/findCommetList")
    @ApiOperation(value = "查询评论列表")
    public Result findCommentList(@PageableDefault(page = 0,size = 10) Pageable pageable, @RequestParam("id")Long id){
        return Result.success(commentService.findCommentList(id, pageable));
    }

    @PostMapping("/addComment")
    @ApiOperation(value = "发表评论")
    @CheckLoginStatus
    public Result addComment(@RequestBody@Valid Comment comment, HttpServletRequest request){
        String access_token = apiService.getAccessToken();
        Boolean aBoolean = SenInfoCheckUtil.cotentFilter(access_token, comment.getContent());
        if (!aBoolean){
            return Result.error(CheckCode.Content_NOT_Illegal);
        }
        commentService.addComment(comment,request);
        return Result.success();
    }

    @DeleteMapping("/deleteCommentById")
    @ApiOperation(value ="删除评论")
    @CheckLoginStatus
    public Result deleteCommentById(@RequestParam("id") Long id){
        commentService.deleteCommentById(id);
        return Result.success();
    }
    @PostMapping("/readComment")
    @ApiOperation(value ="已读评论")
    @CheckLoginStatus
    public Result readComment(@RequestParam("id") Long id){
        commentService.readComment(id);
        return Result.success();
    }

    @PostMapping("/readAllComment")
    @ApiOperation(value = "全部已读")
    @CheckLoginStatus
    public Result readAllComment(HttpServletRequest request){
        commentService.readAllComment(request);
        return Result.success();
    }
    @GetMapping("/findNewestComment")
    @ApiOperation(value = "最新未读信息")
    @CheckLoginStatus
    public Result findNewestComment(HttpServletRequest request){
        return  Result.success(commentService.findNewestComment(request));
    }

    @GetMapping("/findNotReadCommentState")
    @ApiOperation(value = "查询未读评论状态")
    @CheckLoginStatus
    public Result findNotReadCommentState(HttpServletRequest request){
        UserDetails userDetails = redisUtils.getHeader(request);
        Long i = commentMapper.findNotReadCommet(userDetails.getId());
        NoticeCenter noticeCenter = new NoticeCenter();
        if (i>0){
            noticeCenter.setState(0L);
            return Result.success(noticeCenter);
        }
        else {
            noticeCenter.setState(1L);
            return Result.success(noticeCenter);
        }
    }


}
